/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.cib.fintech.entrustedinvestment.biz.service.impl;

import com.cib.fintech.entrustedinvestment.biz.domain.fpss.ProdNavInfo;
import com.cib.fintech.entrustedinvestment.utils.ValidationUtil;
import com.cib.fintech.entrustedinvestment.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.cib.fintech.entrustedinvestment.biz.repository.fpss.ProdNavInfoRepository;
import com.cib.fintech.entrustedinvestment.biz.service.ProdNavInfoService;
import com.cib.fintech.entrustedinvestment.biz.service.dto.fpss.ProdNavInfoDto;
import com.cib.fintech.entrustedinvestment.biz.service.dto.fpss.ProdNavInfoQueryCriteria;
import com.cib.fintech.entrustedinvestment.biz.service.mapstruct.ProdNavInfoMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import com.cib.fintech.entrustedinvestment.utils.PageUtil;
import com.cib.fintech.entrustedinvestment.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author zhangqingkun
* @date 2020-09-28
**/
@Service
@RequiredArgsConstructor
public class ProdNavInfoServiceImpl implements ProdNavInfoService {

    private final ProdNavInfoRepository prodNavInfoRepository;
    private final ProdNavInfoMapper prodNavInfoMapper;

    @Override
    public Map<String,Object> queryAll(ProdNavInfoQueryCriteria criteria, Pageable pageable){
        Page<ProdNavInfo> page = prodNavInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(prodNavInfoMapper::toDto));
    }

    @Override
    public List<ProdNavInfoDto> queryAll(ProdNavInfoQueryCriteria criteria){
        return prodNavInfoMapper.toDto(prodNavInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public ProdNavInfoDto findById(String prodCode) {
        ProdNavInfo prodNavInfo = prodNavInfoRepository.findById(prodCode).orElseGet(ProdNavInfo::new);
        ValidationUtil.isNull(prodNavInfo.getProdCode(),"ProdNavInfo","prodCode",prodCode);
        return prodNavInfoMapper.toDto(prodNavInfo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ProdNavInfoDto create(ProdNavInfo resources) {
        resources.setProdCode(IdUtil.simpleUUID()); 
        return prodNavInfoMapper.toDto(prodNavInfoRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ProdNavInfo resources) {
        ProdNavInfo prodNavInfo = prodNavInfoRepository.findById(resources.getProdCode()).orElseGet(ProdNavInfo::new);
        ValidationUtil.isNull( prodNavInfo.getProdCode(),"ProdNavInfo","id",resources.getProdCode());
        prodNavInfo.copy(resources);
        prodNavInfoRepository.save(prodNavInfo);
    }

    @Override
    public void deleteAll(String[] ids) {
        for (String prodCode : ids) {
            prodNavInfoRepository.deleteById(prodCode);
        }
    }

    @Override
    public void download(List<ProdNavInfoDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ProdNavInfoDto prodNavInfo : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("表名", prodNavInfo.getFpssProdNavInfo());
            map.put("分红前净值", prodNavInfo.getProvNav());
            map.put("分红后净值", prodNavInfo.getAfterNav());
            map.put("分红净值", prodNavInfo.getDividNav());
            map.put("累计净值", prodNavInfo.getTotalNav());
            map.put("万份收益（货基）", prodNavInfo.getIncomeMiloin());
            map.put("七日年化收益", prodNavInfo.getIncomeSeven());
            map.put("管理费率管理费", prodNavInfo.getManageFee());
            map.put("创建时间", prodNavInfo.getCreateTime());
            map.put("更新时间", prodNavInfo.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}